package com.zhiche.lisa.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.google.common.collect.Lists;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.supports.RestfulResponse;
import com.zhiche.lisa.core.utils.HttpClientUtil;
import com.zhiche.lisa.oms.dao.model.bo.ShippingInfoBO;
import com.zhiche.lisa.tms.dao.model.ShipOrder;
import com.zhiche.lisa.tms.dao.model.bo.ErpShipToByLicenseBO;
import com.zhiche.lisa.tms.vo.RestVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class ErpUtil {

    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(ErpUtil.class);

    @Value("${erp.shipToByLicenseURL}")
    private String shipToByLicenseURL;

    @Value("${erp.addTrajectoryURL}")
    private String addTrajectoryURL;

    @Value("${lisa.socketTimeout}")
    private String vehicleQhiueueURL;

    @Value("${erp.getNewestShipByLicenseUrl}")
    private String getNewestShipByLicenseUrl;

    //连接超时时间
    @Value("${erp.vehicleQueueURL}")
    private static int socketTimeout;

    public void shipToByLicense(ErpShipToByLicenseBO erpShipToByLicenseBO) {
        LOGGER.info("shipToByLicense (请求 erp 运抵参数) params:{}", JSONObject.toJSONString(erpShipToByLicenseBO));
        // 发送运抵请求给 erp
        String jsonPara = JSON.toJSONString(erpShipToByLicenseBO);
        List<NameValuePair> headNamePairs = Lists.newArrayList();
        LOGGER.info("shipToByLicense-->运抵推送ERP:url:{},params:{}", shipToByLicenseURL, jsonPara);
        String strReturn = HttpClientUtil.postJson(shipToByLicenseURL, headNamePairs, jsonPara, socketTimeout);
        LOGGER.info("shipToByLicense-->运抵推送ERP success:{}", strReturn);
        Boolean success = null;
        // todo
        //if (!StringUtils.isEmpty(strReturn)) {
        //    RestVO restVO = JSON.parseObject(strReturn, new TypeReference<RestVO<ShipOrder>>() {
        //    });
        //    success = restVO.getSuccess();
        //    String message = restVO.getMessage();
        //    if (!success) {
        //        throw new BaseException("shipToByLicense-->运抵推送ERP 失败" + message);
        //    }
        //} else {
        //    throw new BaseException("shipToByLicense-->运抵推送ERP 未响应");
        //}
    }


    public void positionUpload(ErpShipToByLicenseBO erpShipToByLicenseBO) {
        // 发送在途请求给 erp
        List<NameValuePair> headNamePairs = Lists.newArrayList();
        String strReturn = null;
        try {
            strReturn = HttpClientUtil.postJson(addTrajectoryURL, headNamePairs, JSON.toJSONString(erpShipToByLicenseBO), socketTimeout);
        } catch (Exception e) {
            LOGGER.error("连接erp上报位置超时异常...");
            throw new BaseException(-1, "连接erp上报位置超时异常，请稍后再试！");

        }
        Boolean result = null;
        if (!StringUtils.isEmpty(strReturn)) {
            RestVO restVO = JSON.parseObject(strReturn, new TypeReference<RestVO<ShipOrder>>() {
            });
            result = restVO.getSuccess();
            String message = restVO.getMessage();
            if (!result) {
                LOGGER.error("请求 erp 位置上报异常 error:{}", message);
                throw new BaseException(-1, "位置上报异常" + message);
            }
        } else {
            LOGGER.error("请求 erp 位置上报未响应...");
            throw new BaseException(-1, "请求 erp 位置上报未响应，请稍后再试！");
        }
    }

    /**
     * 调用接口获取调度指令
     *
     */
    public ShippingInfoBO getNewestShipByERP(String license) {
        ShippingInfoBO shippingInfoBO = null;
        // 查询发卡方类型
        List<NameValuePair> headNamePairs = Lists.newArrayList();
        headNamePairs.add(new BasicNameValuePair("Authorization", ""));
        List<NameValuePair> params = Lists.newArrayList();
        params.add(new BasicNameValuePair("vclicense", license));
        String strReturn = HttpClientUtil.get(getNewestShipByLicenseUrl, headNamePairs, params, socketTimeout);
        if (!org.springframework.util.StringUtils.isEmpty(strReturn)) {
            RestfulResponse<ShippingInfoBO> restVO = JSON.parseObject(strReturn, new TypeReference<RestfulResponse<ShippingInfoBO>>() {
            });
            shippingInfoBO = restVO.getData();
        }
        return shippingInfoBO;
    }
}
